--[[
	
	Name: Advanced Questbook
	By: Crypton

]]
AQB_SHOW = false;

function AdvQuestBookMainFrame_CommandHandler(this, cmd)
	if (AQB_SHOW) then
		AQB_SHOW = false;
		AdvQuestBookMainFrame:Hide();
	else
		AQB_SHOW = true;
		AdvQuestBookMainFrame:Show();
	end
end

function AdvQuestBookMainFrame_Toggle()
	AdvQuestBookMainFrame_CommandHandler(false, false);
end

function AdvQuestBookMiniMapShareButton_OnClick()
	AdvQuestBook_QuestPingRequest();
end

function AdvQuestBookMiniBook_OnClick(this, key)
	if (key == "LBUTTON") then
		AdvQuestBookMainFrame_Toggle();
	elseif (key == "RBUTTON") then
		if (AdvQuestBook_Config["minibook"]["status"] == "unlocked") then
			AdvQuestBook_Config["minibook"]["status"] = "locked";
			SaveVariables("AdvQuestBook_Config");
			AdvQuestBookMiniBookFrameCorner:Hide();
			AdvQuestBookMiniBookFrameBorder:Hide();
		else
			AdvQuestBook_Config["minibook"]["status"] = "unlocked";
			SaveVariables("AdvQuestBook_Config");
			AdvQuestBookMiniBookFrameCorner:Show();
			AdvQuestBookMiniBookFrameBorder:Show();
		end
	end
end

function AdvQuestBookCleanQuestDumpData()
	if (AdvQuestBook_Config["settings"][9]["value"]) then
		local i, v;
		local j, k;
		local savecount = 0;
		local cleancount = 0;
		local New_AdvQuestBook_Dumped_Quests = {};
		for i, v in pairs(AdvQuestBook_Dumped_Quests) do
			New_AdvQuestBook_Dumped_Quests[i] = {};
		end
		for i, v in pairs(AdvQuestBook_Dumped_Quests) do
			for j, k in pairs(v) do
				if (not k["turnedin"]) then
					New_AdvQuestBook_Dumped_Quests[i][j] = k;
					savecount = savecount + 1;
				else
					cleancount = cleancount + 1;
				end
			end
		end
		AdvQuestBook_Dumped_Quests = New_AdvQuestBook_Dumped_Quests;
		SaveVariables("AdvQuestBook_Dumped_Quests");
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..AQB_ADDON_NAME.."|r "..AQB_ISTRINGS["AQB_CLEANEDOUT"].." |cFFffffff"..cleancount.."|r "..AQB_ISTRINGS["AQB_QP"].." |cFFffffff"..savecount.."|r "..AQB_ISTRINGS["AQB_QSTS"]..".");
	else
		AdvQuestBook_Dumped_Quests = {};
		SaveVariables("AdvQuestBook_Dumped_Quests");
		AdvQuestBook_Dumped_Quests[AQB_LOCAL] = {};
		SaveVariables("AdvQuestBook_Dumped_Quests");
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..AQB_ADDON_NAME.."|r "..AQB_ISTRINGS["AQB_CLEARDUMP"]..".");
	end
	return;
end

--[[ Begin Config ]]
function AdvQuestBookShareButton_SavePOS()
	local x, y = AdvQuestBookMiniMapShareButtonFrame:GetPos();
	AdvQuestBook_Config["sharebtn"]["x"] = x;
	AdvQuestBook_Config["sharebtn"]["y"] = y;
	SaveVariables("AdvQuestBook_Config");
end

function AdvQuestBookMiniButton_SavePOS()
	local x, y = AdvQuestBookMiniBookFrame:GetPos();
	AdvQuestBook_Config["minibook"]["x"] = x;
	AdvQuestBook_Config["minibook"]["y"] = y;
	SaveVariables("AdvQuestBook_Config");
end

function AdvQuestBookShareMiniButton_OnClick(this, key)
	if (key == "LBUTTON") then
		PlaySoundByPath("sound\\interface\\ui_contextmenu_click.mp3");
		AdvQuestBookMiniMapShareButton_OnClick();
	elseif (key == "RBUTTON") then
		if (IsShiftKeyDown()) then
			UIPanelAnchorFrame_StartMoving(this);
		end
	end
end

function AdvQuestBookMiniButton_Set()
	if (not AddonManager) then
		if (AdvQuestBook_Config["minibook"]["show"]) then
			local AQBMBookX = AdvQuestBook_Config["minibook"]["x"];
			local AQBMBookY = AdvQuestBook_Config["minibook"]["y"];
			AdvQuestBookMiniBookFrame:Show();
			if (not AQB_IsEmpty(AQBMBookX, true) and not AQB_IsEmpty(AQBMBookY, true)) then
				AdvQuestBookMiniBookFrame:SetPos(AQBMBookX, AQBMBookY);
			end
		else
			AdvQuestBookMiniBookFrame:Hide();
		end
	else
		AdvQuestBookMiniBookFrame:Hide();
	end
	if (AQB_IsEmpty(AdvQuestBook_Config["minibook"]["status"], true)) then
		AdvQuestBook_Config["minibook"]["status"] = "unlocked";
	end
	SaveVariables("AdvQuestBook_Config");
	if (AdvQuestBook_Config["minibook"]["status"] == "locked") then
		AdvQuestBookMiniBookFrameCorner:Hide();
		AdvQuestBookMiniBookFrameBorder:Hide();
	else
		AdvQuestBookMiniBookFrameCorner:Show();
		AdvQuestBookMiniBookFrameBorder:Show();
	end
end

function AdvQuestBookConfig_SetChecked()
	local i, j;
	local cbi;
	local cbtbl = table.getn(AdvQuestBook_Config["settings"]);
	local chkbx;
	local qtxt;
	AQB_COUNT_DUMPED = 0;
	AdvQuestBookConfigDBVerText:SetText("QDB: v."..AdvQuestBook_DataVer);
	for i, j in pairs(AdvQuestBook_Dumped_Quests[AQB_LOCAL]) do
		AQB_COUNT_DUMPED = AQB_COUNT_DUMPED + 1;
	end
	if (AQB_COUNT_DUMPED == 1) then
		qtxt = " Quest";
	else
		qtxt = " Quests";
	end
	AdvQuestBookConfigCheckbox8QuestDumpText:SetText("("..AQB_COUNT_DUMPED..qtxt.." "..AQB_ISTRINGS["AQB_DUMPED"]..")");
	for cbi = 1, cbtbl do
		chkbx = getglobal("AdvQuestBookConfigCheckbox"..cbi);
		chkbx:SetChecked(AdvQuestBook_Config["settings"][cbi]["value"]);
		if (cbi == 8) then
			if (AdvQuestBook_Config["settings"][8]["value"]) then
				AdvQuestBookConfigReloadButton:Disable();
				AdvQuestBookConfigReloadUI:SetText(AQB_XML_RELOADUI3);
			else
				AdvQuestBookConfigReloadButton:Enable();
				AdvQuestBookConfigReloadUI:SetText(AQB_XML_RELOADUI2);
			end
		end
	end
	chkbx = getglobal("AdvQuestBookConfigMiniToggle");
	chkbx:SetChecked(AdvQuestBook_Config["minibook"]["show"]);
	if (AddonManager) then
		AdvQuestBookConfigMiniToggle:Disable();
	end
end

function AdvQuestBookConfig_PurgeData()
	AdvQuestBookCleanQuestDumpData();
end

function AdvQuestBookConfig_SaveSettings()
	local cbi;
	local cbtbl = table.getn(AdvQuestBook_Config["settings"]);
	local chkbx;
	for cbi = 1, cbtbl do
		chkbx = getglobal("AdvQuestBookConfigCheckbox"..cbi);
		if (chkbx:IsChecked()) then
			AdvQuestBook_Config["settings"][cbi]["value"] = true;
		else
			AdvQuestBook_Config["settings"][cbi]["value"] = false;
		end
	end
	chkbx = getglobal("AdvQuestBookConfigMiniToggle");
	if (chkbx:IsChecked()) then
		AdvQuestBook_Config["minibook"]["show"] = true;
		AdvQuestBookMiniButton_Set();
	else
		AdvQuestBook_Config["minibook"]["show"] = false;
		AdvQuestBookMiniButton_Set();
	end
	SaveVariables("AdvQuestBook_Config");
end
--[[ End Config ]]

function AdvQuestBook_CloseOpen()
	AdvQuestBookConfig:Hide();
	AdvQuestBookMainFrameHelpResults:Hide();
	AdvQuestBookMainFrameShareResults:Hide();
	AdvQuestBookMainFrameShareQuestResults:Hide();
	AdvQuestBookMainFrameSearchResults:Hide();
	AdvQuestBookMainFrameShareButton:Hide();
	return;
end

function AdvQuestBook_ScrollButtonCTRL(this, cval)
	local scrollbar = getglobal(this);
	local svval = scrollbar:GetValue();
	if (svval == cval) then
		getglobal(this.."ScrollDownButton"):Disable();
	else
		getglobal(this.."ScrollDownButton"):Enable();
	end
	if (svval > 0) then
		getglobal(this.."ScrollUpButton"):Enable();
	else
		getglobal(this.."ScrollUpButton"):Disable();
	end
	if (cval <= 16) then
		getglobal(this.."ScrollDownButton"):Disable();
		getglobal(this.."ScrollUpButton"):Disable();
	end
end

--[[ Begin Search Results Scrolling ]]
function AdvQuestBookSearchResults_UpdateList()
	local i;
	local c = table.getn(AQBMF_SearchResults);
	local sv = AdvQuestBookMainFrameSearchResultsList_ScrollBar:GetValue() + 1;
	local idx;
	local btn;
	local x = 9;
	local y = 13;
	for i = 1, 16 do
		btn = getglobal("AdvQuestBookMainFrameSearchResultsItemButton_"..i);
		btn:Hide();
		btn:UnlockHighlight();
	end
	for i = 1, 16 do
		if (AQBMF_SearchResults[sv]) then
			btn = getglobal("AdvQuestBookMainFrameSearchResultsItemButton_"..i);
			btn:ClearAllAnchors();
			btn.questid = AQBMF_SearchResults[sv]["id"];
			btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameSearchResults", x, (21 * i) + y);
			btn:SetText(AQBMF_SearchResults[sv]["name"]);
			btn:Show();
			sv = sv + 1;
		else
			break;
		end
	end
	if (c > AQB_Search_Max_Window) then
		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameSearchResultsList_ScrollBar", c - AQB_Search_Max_Window);
	else
		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameSearchResultsList_ScrollBar", c);
	end
end

function AdvQuestBookSearchResults_OnValueChanged(this, arg1)
	AdvQuestBookSearchResults_UpdateList();
end

function AdvQuestBookSearchResults_OnMouseWheel(this, delta)
	if (delta > 0) then
		this:SetValue(this:GetValue() - 1);
	else
		this:SetValue(this:GetValue() + 1);
	end
end
--[[ End Search Results Scrolling ]]

--[[ Begin Share Results Scrolling ]]
function AdvQuestBookShareResults_UpdateList()
	local i;
	local c = table.getn(AQB_SharedQuests);
	local sv = AdvQuestBookMainFrameShareResultsList_ScrollBar:GetValue() + 1;
	local idx;
	local btn;
	local x = 9;
	local y = 13;
	for i = 1, 16 do
		btn = getglobal("AdvQuestBookMainFrameShareResultsButton_"..i);
		btn:Hide();
		btn:UnlockHighlight();
	end
	for i = 1, 16 do
		if (AQB_SharedQuests[sv]) then
			btn = getglobal("AdvQuestBookMainFrameShareResultsButton_"..i);
			btn:ClearAllAnchors();
			btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameShareResults", x, (21 * i) + y);
			btn:SetText(AQB_SharedQuests[sv]["name"]);
			btn.pid = sv;
			btn:Show();
			sv = sv + 1;
		else
			break;
		end
	end
--	if (c > AQB_Search_Max_Window) then
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareResultsList_ScrollBar", c - AQB_Search_Max_Window);
--	else
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareResultsList_ScrollBar", c);
--	end
end

function AdvQuestBookShareResults_OnValueChanged(this, arg1)
	AdvQuestBookShareResults_UpdateList();
end

function AdvQuestBookShareResults_OnMouseWheel(this, delta)
	if (delta > 0) then
		this:SetValue(this:GetValue() - 1);
	else
		this:SetValue(this:GetValue() + 1);
	end
end
--[[ End Share Results Scrolling ]]

--[[ Begin Shared Quest Results Scrolling ]]
function AdvQuestBookShareQuestResults_UpdateList()
	local i;
	local c = table.getn(AQB_SharedQuests[AQB_LAST_SHARE]["quests"]);
	local sv = AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:GetValue() + 1;
	local idx;
	local btn;
	local x = 9;
	local y = 13;
	local qid;
	local txt;
	for i = 1, 16 do
		btn = getglobal("AdvQuestBookMainFrameShareQuestResultsButton_"..i);
		btn:Hide();
		btn:UnlockHighlight();
	end
	for i = 1, 16 do
		if (AQB_SharedQuests[sv]) then
			qid = AQB_SharedQuests[AQB_LAST_SHARE]["quests"][sv];
			qid = tonumber(qid);
			txt = AQBQbyID[qid];
			btn = getglobal("AdvQuestBookMainFrameShareQuestResultsButton_"..i);
			btn:ClearAllAnchors();
			btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameShareQuestResults", x, (21 * i) + y);
			btn:SetText(txt);
			btn.questid = qid;
			btn:Show();
			sv = sv + 1;
		else
			break;
		end
	end
--	if (c > AQB_Search_Max_Window) then
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareQuestResultsList_ScrollBar", c - AQB_Search_Max_Window);
--	else
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareQuestResultsList_ScrollBar", c);
--	end
end

function AdvQuestBookShareQuestResults_OnValueChanged(this, arg1)
	AdvQuestBookShareQuestResults_UpdateList();
end

function AdvQuestBookShareQuestResults_OnMouseWheel(this, delta)
	if (delta > 0) then
		this:SetValue(this:GetValue() - 1);
	else
		this:SetValue(this:GetValue() + 1);
	end
end
--[[ End Shared Quest Results Scrolling ]]

function AdvQuestBookMainFrame_SearchEditOnEnterPressed()
	AdvQuestBook_CloseOpen();
	AdvQuestBookMainFrame_SearchQuest();
end

function AdvQuestBookMainFrame_OnHyperlinkClick(this, link, key)
	Hyperlink_Assign(link, key);
end

function AdvQuestBookMainFrame_OnHyperlinkEnter(this, link)
	SetHyperlinkCursor(true);
end

function AdvQuestBookMainFrame_OnHyperlinkLeave(this, link)
	SetHyperlinkCursor(false);
end

function AdvQuestBookMainFrame_OnEvent(this, event)
	if (event == "VARIABLES_LOADED") then
		AdvQuestBookMiniMapShareButton:RegisterForClicks("LeftButton", "RightButton");
		AdvQuestBookMiniBookButton:RegisterForClicks("LeftButton", "RightButton");
		SLASH_AQB1 = "/aqb";
		SlashCmdList["AQB"] = AdvQuestBookMainFrame_CommandHandler;
		-- Begin AddonManager Support
		if AddonManager then
			AddonManager.RegisterAddon(
				"|cffFFFF00Advanced Quest Book|r",
				"\n"..AQB_ISTRINGS["AQB_AMINFO"],
				"Interface\\AddOns\\AdvQuestBook\\images\\aqb.png",
				"Information",
				false,
				"/aqb",
				AdvQuestBookMiniButton,
				AdvQuestBookMainFrame_Toggle,
				"v."..AQB_VER_NUM,
				"Crypton",
				false,
				false
			);
		-- End AddonManager Support
		else
			DEFAULT_CHAT_FRAME:AddMessage(AQB_TITLE_PAGE.." by Crypton Loaded...");
			DEFAULT_CHAT_FRAME:AddMessage("AddonManager "..AQB_ISTRINGS["AQB_AMND1"]);
			DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_AMND2"].." |cFFffffffAdvanced Questbook|r.");
			DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_AMND4"]..": |cFFffffff/aqb|r "..AQB_ISTRINGS["AQB_AMND3"].." AQB "..AQB_ISTRINGS["AQB_AMND5"]..".");
		end
		AdvQuestBookMiniButton_Set();
	end
end

function AdvQuestBookMainFrame_OnLoad(this)
	this:RegisterEvent("VARIABLES_LOADED");
	tdetailh = 0;
	AQB_LAST_SHARE = 0;
	AQBMF_SearchResults = {};
	local slider = UIPanelScrollFrame_GetScrollBar(AdvQuestBookMainFrameQuestDetailScrollFrame);
	slider:ClearAllAnchors();
	slider:SetAnchor("TOPLEFT", "TOPRIGHT", AdvQuestBookMainFrameQuestDetailBackdrop, -13, 45);
	slider:SetAnchor("BOTTOMRIGHT", "BOTTOMRIGHT", AdvQuestBookMainFrameQuestDetailBackdrop, -1, -19);
	AdvQuestBookMainFrameQuestDesc:Hide();
	AdvQuestBookMainFrameQuestReward:Hide();
	AdvQuestBookMainFrameQuestLocation:Hide();
	local i;
	local x = 9;
	local y = 15;
	local hc = table.getn(AQB_HELP_TOPICS);
	for i = 1, hc do
		local btn = getglobal("AdvQuestBookMainFrameHelpResultsItemButton_"..i);
		btn:Hide();
		local btn = getglobal("AdvQuestBookMainFrameHelpResultsItemButton_"..i);
		btn:ClearAllAnchors();
		btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameHelpResults", x, (21 * i) + y);
		btn:SetText(AQB_HELP_TOPICS[i]);
		btn:Show();
	end
	AQB_Search_Max_Window = 16;
	AQB_Search_Max = 100;
	AdvQuestBookMainFrameSearchResultsList_ScrollBar:SetValueStepMode("INT");
	AdvQuestBookMainFrameSearchResultsList_ScrollBar:SetMaxValue(0);
	AdvQuestBookMainFrameSearchResultsList_ScrollBar:SetValue(0);
	AdvQuestBookMainFrameShareResultsList_ScrollBar:SetValueStepMode("INT");
	AdvQuestBookMainFrameShareResultsList_ScrollBar:SetMaxValue(0);
	AdvQuestBookMainFrameShareResultsList_ScrollBar:SetValue(0);
	AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:SetValueStepMode("INT");
	AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:SetMaxValue(0);
	AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:SetValue(0);
	getglobal("AdvQuestBookMainFrameSearchResultsList_ScrollBar".."ScrollUpButton"):Disable();
	getglobal("AdvQuestBookMainFrameSearchResultsList_ScrollBar".."ScrollDownButton"):Disable();
--	getglobal("AdvQuestBookMainFrameShareResultsList_ScrollBar".."ScrollUpButton"):Disable();
--	getglobal("AdvQuestBookMainFrameShareResultsList_ScrollBar".."ScrollDownButton"):Disable();
--	getglobal("AdvQuestBookMainFrameShareQuestResultsList_ScrollBar".."ScrollUpButton"):Disable();
--	getglobal("AdvQuestBookMainFrameShareQuestResultsList_ScrollBar".."ScrollDownButton"):Disable();
end

function AdvQuestBookMainFrameShareResult_OnClick(this)
	AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:SetMaxValue(0);
	local i;
	local x = 9;
	local y = 15;
	local tmpbtn = getglobal(this);
	local resnum = tmpbtn.pid;
	local qid;
	local qname;
	local questcount = table.getn(AQB_SharedQuests[resnum]["quests"]);
	AQB_LAST_SHARE = resnum;
	for i = 1, 16 do
		if (AQB_SharedQuests[resnum]["quests"][i]) then
			local btn = getglobal("AdvQuestBookMainFrameShareQuestResultsButton_"..i);
			qid = AQB_SharedQuests[resnum]["quests"][i];
			qid = tonumber(qid);
			if (not AQB_IsEmpty(AQBQbyID[qid], true)) then
				qname = AQBQbyID[qid];
			else
				qname = AQB_ISTRINGS["AQB_UNKNOWN_QUEST"];
			end
			btn:ClearAllAnchors();
			btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameShareQuestResults", x, (21 * i) + y);
			btn.questid = qid;
			btn:SetText(qname);
			btn:Show();
		else
			break;
		end
	end
	AdvQuestBook_CloseOpen();
	AdvQuestBookMainFrameShareQuestResults:Show();
	AdvQuestBook_SetSharedIndexes();
	if (questcount > AQB_Search_Max_Window) then
		AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:SetMaxValue(questcount - AQB_Search_Max_Window);
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareQuestResultsList_ScrollBar", questcount - AQB_Search_Max_Window);
	else
		AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:SetMaxValue(0);
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareQuestResultsList_ScrollBar", questcount);
	end
	AdvQuestBookMainFrameShareQuestResultsList_ScrollBar:ResetFrameOrder();
end

function AdvQuestBookMainFrameShareQuest_OnClick(this)
	local rensum = string.gsub(this, "AdvQuestBookMainFrameShareQuestResults_", "");
	resnum = tonumber(resnum);
	local qid = getglobal(this);
	qid = qid.questid;
	local restext = AQBQbyID[qid];
	local qtext = AdvQuestBook_GlobalSharedIndex[qid]["info"]["desclong"];
	local qgold = AdvQuestBook_GlobalSharedIndex[qid]["rewards"]["Gold"];
	local qxp = AdvQuestBook_GlobalSharedIndex[qid]["rewards"]["Exp"];
	local qtp = AdvQuestBook_GlobalSharedIndex[qid]["rewards"]["TP"];
	local sNPCid = AdvQuestBook_GlobalSharedIndex[qid]["start"]["npcid"];
	local sNPCname = AdvQuestBook_GlobalSharedIndex[qid]["start"]["npc"];
	local sMap = AdvQuestBook_GlobalSharedIndex[qid]["start"]["mapid"];
	local eMap = AdvQuestBook_GlobalSharedIndex[qid]["end"]["mapid"];
	local sMap = AQBMapID[sMap];
	local eMap = AQBMapID[eMap];
	local totalh = 0;
	if (sMap == nil) then
		sMap = AQB_ISTRINGS["AQB_UNKNOWN"];
	end
	if (eMap == nil) then
		eMap = AQB_ISTRINGS["AQB_UNKNOWN"];
	end
	local eNPCid = AdvQuestBook_GlobalSharedIndex[qid]["end"]["npcid"];
	local eNPCname = AdvQuestBook_GlobalSharedIndex[qid]["end"]["npc"];
	local qrewards = AQB_ISTRINGS["AQB_GOLD"]..": "..qgold.."\n"..AQB_ISTRINGS["AQB_XP"]..": "..qxp.."\n"..AQB_ISTRINGS["AQB_TP"]..": "..qtp;
	local locations = AQB_ISTRINGS["AQB_START"]..": |Hnpc:"..sNPCid.."|h|cff0022ee"..sNPCname.."|r|h - "..AQB_ISTRINGS["AQB_MAP"]..": "..sMap.."\n"..AQB_ISTRINGS["AQB_END"]..": |Hnpc:"..eNPCid.."|h|cff0022ee"..eNPCname.."|r|h - "..AQB_ISTRINGS["AQB_MAP"]..": "..eMap;
	-- Name
	AdvQuestBookMainFrameQuestName:SetText(" ");
	AdvQuestBookMainFrameQuestName:SetText(restext);
	-- Description
	AdvQuestBookMainFrameQuestDescMessage:ClearText();
	AdvQuestBookMainFrameQuestDescMessageText:SetText("");
	AdvQuestBookMainFrameQuestDescMessage:AddMessage(qtext);
	AdvQuestBookMainFrameQuestDescMessageText:SetText(qtext);
	local h = AdvQuestBookMainFrameQuestDescMessageText:GetHeight();
	AdvQuestBookMainFrameQuestDescMessage:SetHeight(h);
	totalh = h;
	-- Rewards
	AdvQuestBookMainFrameQuestRewardMessage:ClearText();
	AdvQuestBookMainFrameQuestRewardMessageText:SetText("");
	AdvQuestBookMainFrameQuestRewardMessage:AddMessage(qrewards);
	AdvQuestBookMainFrameQuestRewardMessageText:SetText(qrewards);
	local h = AdvQuestBookMainFrameQuestRewardMessageText:GetHeight();
	AdvQuestBookMainFrameQuestRewardMessage:SetHeight(h);
	totalh = totalh + h;
	-- Location
	AdvQuestBookMainFrameQuestLocationMessage:ClearText();
	AdvQuestBookMainFrameQuestLocationMessageText:SetText("");
	AdvQuestBookMainFrameQuestLocationMessage:AddMessage(locations);
	AdvQuestBookMainFrameQuestLocationMessageText:SetText(locations);
	local h = AdvQuestBookMainFrameQuestLocationMessageText:GetHeight();
	AdvQuestBookMainFrameQuestLocationMessage:SetHeight(h);
	totalh = totalh + h;
--	totalh = totalh / 4;
	local scrollbar = getglobal("AdvQuestBookMainFrameQuestDetailScrollFrameScrollBar");
	scrollbar:SetMinMaxValues(0, totalh);
	scrollbar:SetValue(0);
	AdvQuestBookMainFrameQuestDetailScrollFrame:UpdateScrollChildRect();
	AdvQuestBookMainFrameQuestDesc:Show();
	AdvQuestBookMainFrameQuestReward:Show();
	AdvQuestBookMainFrameQuestLocation:Show();
end

function AdvQuestBookMainFrameShareButton_ShowShared()
	AdvQuestBookMainFrameShareResultsList_ScrollBar:SetMaxValue(0);
	local i;
	local x = 9;
	local y = 11;
	local hc = table.getn(AQB_SharedQuests);
	local btn;
	for i = 1, 16 do
		btn = getglobal("AdvQuestBookMainFrameShareResultsButton_"..i);
		btn:Hide();
	end
	if (hc > 0) then
		for i = 1, 16 do
			if (AQB_SharedQuests[i]) then
				btn = getglobal("AdvQuestBookMainFrameShareResultsButton_"..i);
				btn:ClearAllAnchors();
				btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameShareResults", x, (21 * i) + y);
				btn:SetText(AQB_SharedQuests[i]["name"]);
				btn.pid = i;
				btn:Show();
			else
				break;
			end
		end
	end
	if (hc > AQB_Search_Max_Window) then
		AdvQuestBookMainFrameShareResultsList_ScrollBar:SetMaxValue(hc - AQB_Search_Max_Window);
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareResultsList_ScrollBar", hc - AQB_Search_Max_Window);
	else
		AdvQuestBookMainFrameShareResultsList_ScrollBar:SetMaxValue(0);
--		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameShareResultsList_ScrollBar", hc);
	end
	AdvQuestBookMainFrameShareResultsList_ScrollBar:ResetFrameOrder();
end

function AdvQuestBookMainFrameShareBack_OnClick()
	AdvQuestBook_CloseOpen();
	AdvQuestBookMainFrameShareResults:Show();
	AdvQuestBookMainFrameShareButton:SetText(AQB_XML_HIDESHARE);
	AdvQuestBookMainFrameShareButton:Show();
end

function AdvQuestBookMainFrameHelpButton_OnClick(this)
	local resnum = string.gsub(this, "AdvQuestBookMainFrameHelpResultsItemButton_", "");
	resnum = tonumber(resnum);
	local i;
	local topiccount = table.getn(AQB_HELP_TOPICS);
	local restext = AQB_HELP_TEXT[resnum];
	local totalh = 0;
	local obj;
	for i = 1, topiccount do
		obj = getglobal("AdvQuestBookMainFrameHelpResultsItemButton_"..i);
		obj:UnlockHighlight();
	end
	obj = getglobal(this);
	obj:LockHighlight();
	AdvQuestBookMainFrameQuestName:SetText("");
	AdvQuestBookMainFrameQuestDescMessage:ClearText();
	AdvQuestBookMainFrameQuestRewardMessage:ClearText();
	AdvQuestBookMainFrameQuestLocationMessage:ClearText();
	AdvQuestBookMainFrameQuestDesc:Show();
	AdvQuestBookMainFrameQuestReward:Hide();
	AdvQuestBookMainFrameQuestLocation:Hide();
	AdvQuestBookMainFrameQuestDescMessage:SetHeight(0);
	AdvQuestBookMainFrameQuestRewardMessage:SetHeight(0);
	AdvQuestBookMainFrameQuestLocationMessage:SetHeight(0);
	AdvQuestBookMainFrameQuestDescMessage:AddMessage(restext);
	AdvQuestBookMainFrameQuestDescMessageText:SetText(restext);
	local h = AdvQuestBookMainFrameQuestDescMessageText:GetHeight();
	AdvQuestBookMainFrameQuestDescMessage:SetHeight(h);
	totalh = totalh + h;
--	totalh = totalh / 4;
	local scrollbar = getglobal("AdvQuestBookMainFrameQuestDetailScrollFrameScrollBar");
	scrollbar:SetMinMaxValues(0, totalh);
	scrollbar:SetValue(0);
	AdvQuestBookMainFrameQuestDetailScrollFrame:UpdateScrollChildRect();
end

function AdvQuestBookMainFrame_SearchResultOnClick(this)
	local i;
	local obj;
	for i = 1, 16 do
		obj = getglobal("AdvQuestBookMainFrameSearchResultsItemButton_"..i);
		obj:UnlockHighlight();
	end
	obj = getglobal(this);
	obj:LockHighlight();
	local qid = obj.questid;
	local restext = AdvQuestBook_GlobalSearchIndex[qid]["name"];
	local qtext = AdvQuestBook_GlobalSearchIndex[qid]["info"]["desclong"];
	local qgold = AdvQuestBook_GlobalSearchIndex[qid]["rewards"]["Gold"];
	local qxp = AdvQuestBook_GlobalSearchIndex[qid]["rewards"]["Exp"];
	local qtp = AdvQuestBook_GlobalSearchIndex[qid]["rewards"]["TP"];
	local sNPCid = AdvQuestBook_GlobalSearchIndex[qid]["start"]["npcid"];
	local sNPCname = AdvQuestBook_GlobalSearchIndex[qid]["start"]["npc"];
	local sMap = AdvQuestBook_GlobalSearchIndex[qid]["start"]["mapid"];
	local eMap = AdvQuestBook_GlobalSearchIndex[qid]["end"]["mapid"];
	local sMap = AQBMapID[sMap];
	local eMap = AQBMapID[eMap];
	local totalh = 0;
	if (sMap == nil) then
		sMap = AQB_ISTRINGS["AQB_UNKNOWN"];
	end
	if (eMap == nil) then
		eMap = AQB_ISTRINGS["AQB_UNKNOWN"];
	end
	local eNPCid = AdvQuestBook_GlobalSearchIndex[qid]["end"]["npcid"];
	local eNPCname = AdvQuestBook_GlobalSearchIndex[qid]["end"]["npc"];
	local qrewards = AQB_ISTRINGS["AQB_GOLD"]..": "..qgold.."\n"..AQB_ISTRINGS["AQB_XP"]..": "..qxp.."\n"..AQB_ISTRINGS["AQB_TP"]..": "..qtp;
	local locations = AQB_ISTRINGS["AQB_START"]..": |Hnpc:"..sNPCid.."|h|cff0022ee"..sNPCname.."|r|h - "..AQB_ISTRINGS["AQB_MAP"]..": "..sMap.."\n"..AQB_ISTRINGS["AQB_END"]..": |Hnpc:"..eNPCid.."|h|cff0022ee"..eNPCname.."|r|h - "..AQB_ISTRINGS["AQB_MAP"]..": "..eMap;
	-- Name
	AdvQuestBookMainFrameQuestName:SetText(" ");
	AdvQuestBookMainFrameQuestName:SetText(restext);
	-- Description
	AdvQuestBookMainFrameQuestDescMessage:ClearText();
	AdvQuestBookMainFrameQuestDescMessageText:SetText("");
	AdvQuestBookMainFrameQuestDescMessage:AddMessage(qtext);
	AdvQuestBookMainFrameQuestDescMessageText:SetText(qtext);
	local h = AdvQuestBookMainFrameQuestDescMessageText:GetHeight();
	AdvQuestBookMainFrameQuestDescMessage:SetHeight(h);
	totalh = h;
	-- Rewards
	AdvQuestBookMainFrameQuestRewardMessage:ClearText();
	AdvQuestBookMainFrameQuestRewardMessageText:SetText("");
	AdvQuestBookMainFrameQuestRewardMessage:AddMessage(qrewards);
	AdvQuestBookMainFrameQuestRewardMessageText:SetText(qrewards);
	local h = AdvQuestBookMainFrameQuestRewardMessageText:GetHeight();
	AdvQuestBookMainFrameQuestRewardMessage:SetHeight(h);
	totalh = totalh + h;
	-- Location
	AdvQuestBookMainFrameQuestLocationMessage:ClearText();
	AdvQuestBookMainFrameQuestLocationMessageText:SetText("");
	AdvQuestBookMainFrameQuestLocationMessage:AddMessage(locations);
	AdvQuestBookMainFrameQuestLocationMessageText:SetText(locations);
	local h = AdvQuestBookMainFrameQuestLocationMessageText:GetHeight();
	AdvQuestBookMainFrameQuestLocationMessage:SetHeight(h);
	totalh = totalh + h;
--	totalh = totalh / 4;
	local scrollbar = getglobal("AdvQuestBookMainFrameQuestDetailScrollFrameScrollBar");
	scrollbar:SetMinMaxValues(0, totalh);
	scrollbar:SetValue(0);
	AdvQuestBookMainFrameQuestDetailScrollFrame:UpdateScrollChildRect();
	AdvQuestBookMainFrameQuestDesc:Show();
	AdvQuestBookMainFrameQuestReward:Show();
	AdvQuestBookMainFrameQuestLocation:Show();
end

function AdvQuestBookMainFrame_UpdateSearchResultList()
	local i;
	local c = table.getn(AQBMF_SearchResults);
	local x = 9;
	local y = 13;
	AdvQuestBookMainFrameQuestName:SetText(" ");
	AdvQuestBookMainFrameQuestDescMessage:ClearText();
	AdvQuestBookMainFrameQuestDescMessageText:SetText("");
	AdvQuestBookMainFrameQuestDescMessage:SetHeight(0);
	AdvQuestBookMainFrameQuestRewardMessage:ClearText();
	AdvQuestBookMainFrameQuestRewardMessageText:SetText("");
	AdvQuestBookMainFrameQuestRewardMessage:SetHeight(0);
	AdvQuestBookMainFrameQuestLocationMessage:ClearText();
	AdvQuestBookMainFrameQuestLocationMessageText:SetText("");
	AdvQuestBookMainFrameQuestLocationMessage:SetHeight(0);
	AdvQuestBookMainFrameQuestDesc:Hide();
	AdvQuestBookMainFrameQuestReward:Hide();
	AdvQuestBookMainFrameQuestLocation:Hide();
	AdvQuestBook_SetSearchIndexes();
	for i = 1, 16 do
		local btn = getglobal("AdvQuestBookMainFrameSearchResultsItemButton_"..i);
		btn:Hide();
	end
	for i = 1, c do
		local btn = getglobal("AdvQuestBookMainFrameSearchResultsItemButton_"..i);
		btn:ClearAllAnchors();
		btn.questid = AQBMF_SearchResults[i]["id"];
		btn:SetAnchor("TOPLEFT", "TOPLEFT", "AdvQuestBookMainFrameSearchResults", x, (21 * i) + y);
		btn:SetText(AQBMF_SearchResults[i]["name"]);
		btn:Show();
		if (i == 16) then
			break;
		end
	end
	if (c > AQB_Search_Max_Window) then
		AdvQuestBookMainFrameSearchResultsList_ScrollBar:SetMaxValue(c - AQB_Search_Max_Window);
		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameSearchResultsList_ScrollBar", c - AQB_Search_Max_Window);
	else
		AdvQuestBookMainFrameSearchResultsList_ScrollBar:SetMaxValue(0);
		AdvQuestBook_ScrollButtonCTRL("AdvQuestBookMainFrameSearchResultsList_ScrollBar", c);
	end
	AdvQuestBookMainFrameSearchResultsList_ScrollBar:ResetFrameOrder();
end

function AdvQuestBookMainFrame_SearchQuest()
	AQBMF_SearchResults = {};
	local AQB_Search_Text = AdvQuestBookMainFrameSearchOptionsSearchBox:GetText();
	if (string.len(AQB_Search_Text) < 3) then
		DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_SEARCHSHORT1"].." 3 "..AQB_ISTRINGS["AQB_SEARCHSHORT2"].."...");
		return;
	end
	DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_SEARCHING"]..": \"|cFFffffff"..AQB_Search_Text.."|r\"");
	local AQB_SLower = string.lower(AQB_Search_Text);
	local AQB_Total_Count = 0;
	local aqba, aqbb;
	for aqba, aqbb in pairs(AQBQbyName) do
		if (AQB_Total_Count > AQB_Search_Max) then
			DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_MORETHAN1"].." |cFFffffff"..AQB_Search_Max.."|r "..AQB_ISTRINGS["AQB_MORETHAN2"]..".");
			break;
		end
		local AQB_Cur_Key_Lower = string.lower(aqba);
		if (string.match(AQB_Cur_Key_Lower, AQB_SLower)) then
			AQB_Total_Count = AQB_Total_Count + 1;
			AQBMF_SearchResults[AQB_Total_Count] = {
				["name"] = aqba,
				["id"] = aqbb,
			};
		end
	end
	if (AQB_Total_Count > AQB_Search_Max_Window) then
		DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_FOUND"].." |cFFffffff"..AQB_Total_Count.."|r "..AQB_ISTRINGS["AQB_RESULTSFOR"].." \"|cFFffffff"..AQB_Search_Text.."|r\"");
	else
		if (AQB_Total_Count > 0) then
			DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_FOUND"].." |cFFffffff"..AQB_Total_Count.."|r "..AQB_ISTRINGS["AQB_RESULTSFOR"].." \"|cFFffffff"..AQB_Search_Text.."|r\"");
		else
			DEFAULT_CHAT_FRAME:AddMessage(AQB_ISTRINGS["AQB_NORESULTS"].." \"|cFFffffff"..AQB_Search_Text.."|r\"");
		end
	end
	AQB_SearchCountLastTotal = AQB_Total_Count;
	AdvQuestBookMainFrame_UpdateSearchResultList();
end
